package com.sojoys.artifact.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sojoys.artifact.config.Constants;
import com.sojoys.artifact.config.Modules;
import com.sojoys.artifact.config.Plugins;
import com.sojoys.artifact.constant.ServerStatusEnum;

public class IServer {
	static final Logger log = LoggerFactory.getLogger(IServer.class);
	// 常量
	private static final Constants constants = new Constants();
	// 插件
	private static final Plugins plugins = new Plugins();
	// 处理器
	private static final Modules modules = new Modules(){public Modules config() {return this;}};
	
	private static IConfig config;
	// 服务器状态
	public static volatile ServerStatusEnum SERVER_STATUS = ServerStatusEnum.STOP;
	
	private IServer(){
		
	}
	/**
	 * 设置配置
	 * @param iconfig
	 */
	public static void setIConfig(IConfig iconfig){
		config = iconfig;
	}

	public static Constants getConstants() {
		return constants;
	}
	
	public static Plugins getPlugins() {
		return plugins;
	}
	
	public static Modules getModules() {
		return modules;
	}
	
	
	public static void start() {
		SERVER_STATUS = ServerStatusEnum.STARTING;
		if (config==null) {
			log.error("iconfig net fond");
			return;
		}
		// 初始化
		config.configConstant(constants);
		// 注册module
		config.configModules(modules);
		// 添加插件
		config.configPlugin(plugins);
		// 启动插件
		plugins.plugins(true);
		// 启动完毕后调用
		config.afterServerStart();
		// 修改服务器状态
		if (getConstants().isDevMode()){
			SERVER_STATUS = ServerStatusEnum.RUNNING;
		}else{
			SERVER_STATUS = ServerStatusEnum.AEGIS;
		}
		config.startSucceed();
	}
	
	public static boolean stop() {
		log.info("-> Server Stop");
		IServer.SERVER_STATUS = ServerStatusEnum.STOPPING;
		log.info("-> Stop Plugins");
		// 停止插件
		plugins.plugins(false);
		log.info("-> BeforeServerStop");
		// 停止前调用
		config.beforeServerStop();
		log.info("-> Server Stop Finish");
		// 修改服务器状态
		SERVER_STATUS = ServerStatusEnum.STOP;
		return true;
	}
	
	
}